<?php
namespace Admin\Controller;

class SnatchController extends AdminController{

	public function index(){
		$keyword = I('keyword');
		$map = array();
		if (!empty($keyword)) {
			$map['title'] = ['like','%'.$keyword.'%'];
		}
        $list   = $this->lists('Snatch', $map);
		$this->assign('prize_type',C('prize_type'));
        $this->assign('_list', $list);
        $this->meta_title = '夺宝活动列表';
        $this->display();
	}
	/**
	 * 添加活动
	 */
	public function add(){
		if(IS_POST){
			$data = I('post.');
			if ($data['prize'] == 'coin') {
				$data['goods_price'] = 0;
			}
			$data['start_time'] = strtotime($data['start_time']);
			$data['end_time'] = strtotime($data['end_time']);
			if ($data['start_time'] >= $data['end_time']) {
				$this->error("请正确选择活动时间");
			}
			if ($data['start_time'] <= NOW_TIME && $data['end_time'] > NOW_TIME) {
				$data['status'] = 1;
			}elseif ($data['end_time'] < NOW_TIME) {
				$data['status'] = 2;
			}
			$data['add_time']	=	time();
			$result = M("snatch")->add($data);
			if ($result !== false) {
				$this->success("添加成功",U('index'));
			}else{
				$this->error("添加失败");
			}
		}
		$this->assign('prize_type',C('prize_type'));
		$this->display();
	}
	public function edit(){
		$id = I('id');
		if(IS_POST){
			$data = I('post.');
			if ($data['prize'] == 'coin') {
				$data['goods_price'] = 0;
			}
			$data['start_time'] = strtotime($data['start_time']);
			$data['end_time'] = strtotime($data['end_time']);
			if ($data['start_time'] >= $data['end_time']) {
				$this->error("请正确选择活动时间");
			}
			if ($data['start_time'] <= NOW_TIME && $data['end_time'] > NOW_TIME) {
				$data['status'] = 1;
			}elseif ($data['end_time'] < NOW_TIME) {
				$data['status'] = 2;
			}
			$result = M("snatch")->where(array('id'=>$data['id']))->save($data);
			if ($result !== false) {
				$this->success("修改成功",U('index'));
			}else{
				$this->error("修改失败");
			}
		}
		$info = M('snatch')->where(array('id'=>$id))->find();
		$this->assign('prize_type',C('prize_type'));
		$this->assign('info',$info);
		$this->display();
	}
	public function delete(){
		$id = I('id');
		$info = M('snatch')->where(array('id'=>$id))->find();
		$now = time();
		if ($info['start_time'] <= $now && $info['end_time'] > $now) {
			$this->error("活动进行中，无法删除");
		}
		$result = M('snatch')->delete($id);
		if ($result !== false) {
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
	}
	public function banner(){
        $map['position']=array('eq','snatch_banner');
        $list=M('ad')->where($map)->select();
        $this->assign('_list',$list);
        $this->meta_title = "人品大爆发轮播图";
        $this->display();
	}
	public function ad_add(){
        $Ad = D('ad');
        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'sort'=>I('sort'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'create_time'=>NOW_TIME,'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $id = $Ad->add($lunbo);
            $id > 0 ? $this->success('新增成功',U('banner')) : $this->error('新增失败');
        }
		$this->display();
	}
	public function ad_edit(){
        $Ad = D('ad');
        $id = I('id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');

        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'sort'=>I('sort'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $status = $Ad->where(array('id'=>$id))->save($lunbo);
            $status !== false ? $this->success('修改成功', U('banner')) : $this->error('修改失败');
        }

        $this->assign('info', $info);
        $this->meta_title = "编辑轮播图";
        $this->display('ad_add',U('banner'));
	}

    public function ad_del(){
        $id = I('id');
        if (! $id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_ad = M("ad");
        $result = $m_ad->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功');
        $this->error('删除失败');
    }
    public function setting(){
    	if (IS_POST) {
    		$data = I('setting');
    		if (!empty($data)) {
    			foreach ($data as $key => $value) {
		            $map = array('name' => $key);
		            M('Config')->where($map)->setField('value', $value);
    			}
    		}
    		S('DB_CONFIG_DATA',null);
        	$this->success("设置成功");
    	}
    	$this->assign("app_url",C('APP_URL'));
    	$this->assign("snatch_rule",C('SNATCH_RULE'));
    	$this->assign("winner_ids",C('WINNER_IDS'));
    	$this->assign("wechat_no",C('WECHAT_NO'));
    	$this->display();
    }
    /**
     * 中奖列表
     * @return [type] [description]
     */
    public function winner_list(){
    	$map = array(
    		'status'	=>	2,
    		'lottery_uid'	=>	array('gt',0)
    	);
    	$list = $this->lists('snatch',$map);
		$this->assign('prize_type',C('prize_type'));
    	$this->assign('_list',$list);
    	$this->display();
    }
}